package test4;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author Jun jie Yi
 * 2022/5/1 10:20
 */
public class Demo2 {
    static String[][] number = {{}, {},
            {"a", "b", "c"},
            {"d", "e", "f"},
            {"g", "h", "i"},
            {"j", "k", "l"},
            {"m", "n", "o"},
            {"p", "q", "r", "s"},
            {"t", "u", "v"},
            {"w", "x", "y", "z"}
    };

    public static void main(String[] args) {
        List<String> list = letterCombinations("2");
        System.out.println(list);
    }

    public static List<String> letterCombinations(String digits) {
        List<String> res = new ArrayList<>();
        if ("".equals(digits) || digits == null) return res;
        String[] arr = digits.split("");
        getList(res, arr, 0, new StringBuilder());
        return res;
    }

    //使用dfs
    public static void getList(List<String> res, String[] arr, int index, StringBuilder sb) {
        //终止条件
        if (index == arr.length) {
            res.add(sb.toString());
            return;
        }
        int x = Integer.parseInt(arr[index]);
        String[] s = number[x];
        for (int i = 0; i < s.length; i++) {
            getList(res, arr, index + 1, new StringBuilder(sb).append(s[i]));
        }
    }
}
